<template>
	<div class="boxStyle">
		<el-row type="flex" class="row-bg" justify="start" style="margin-bottom: 10px;">
			<el-col :span="1">
				<el-button type="warning" size="small" v-if="addShow!==-1" @click="add()" icon="el-icon-plus">新增</el-button>
			</el-col>
		</el-row>
		<el-table ref="multipleTable" stripe border :data="channelLists" tooltip-effect="dark" style="margin-bottom: 10px;width: 100%">
			<el-table-column type="selection" width="55"></el-table-column>
			<el-table-column prop="id" label="序号" width="55"></el-table-column>
			<el-table-column prop="name" label="渠道商名称" width="120"></el-table-column>
			<el-table-column prop="createTime" label="创建时间" width="200" :formatter="dateFormat"></el-table-column>
			<el-table-column prop="cType" label="平台抽成方式" width="150">
				<template slot-scope="scope">
					<span v-if="scope.row.cType==0">关闭</span>
					<span v-if="scope.row.cType==1">每单固定抽成</span>
					<span v-if="scope.row.cType==2">按订单比例抽成</span>
				</template>
			</el-table-column>
			<el-table-column prop="mobile" label="支付手机号" width="150"></el-table-column>
			<el-table-column prop="remark" label="备注" width="120"></el-table-column>
			<el-table-column label="操作" min-width="300">
				<template slot-scope="scope">
					<el-button size="mini" type="primary" v-if="updateShow!==-1" @click="update(scope.$index,scope.row.id)">编辑</el-button>
				</template>
			</el-table-column>
		</el-table>
		<el-pagination background @current-change="handleCurrentChange" :current-page="currentPage" :page-size="size" layout="prev, pager, next,total, slot" :total="totals">
			<span style="margin-top: -2px;color: #606266;font-weight: 100;">第<el-input type="text" v-model="inputPage" style="width: 60px;margin: 0 5px;"/>页<el-button size="mini" style="background: #409EFF; color: #fff;margin:0 5px;" @click="handleCurrentChange(inputPage)">跳转</el-button><span>总计{{totalPage}}页</span></span>
		</el-pagination>
		<div class="modal fade" id="channelAdd" tabindex="-1" role="dialog" data-keyboard="false">
			<div class="modal-dialog" role="document">
				<div class="modal-content" style="width:800px;height: 540px;">
					<div class="modal-header" style="border-radius: 4px;">
						<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
						<ul class="nav nav-tabs" role="tablist">
							<li role="presentation" class="active">
								<a href="#qusInfo" aria-controls="qusInfo" role="tab" data-toggle="tab">新增/编辑</a>
							</li>
						</ul>
					</div>
					<div class="modal-body" style="padding-right: 60px;height:410px;overflow: hidden;overflow-y: auto;">
						<div class="tab-content">
							<div role="tabpanel" class="tab-pane active" id="qusInfo">
								<el-form :model="channelModel" :rules="channelRul" ref="channelModel" label-width="100px" class="demo-ruleForm" @submit.native.prevent>
									<input type="hidden" name="id" v-model="id">
									<el-col :span="12">
										<el-form-item label="渠道商名称 :" prop="name" :rules="MUSTINFO">
											<el-input v-model="channelModel.name" size="small"></el-input>
										</el-form-item>
									</el-col>
									<el-col :span="12">
										<el-form-item label="备注:" prop="remark">
											<el-input v-model="channelModel.remark" size="small"></el-input>
										</el-form-item>
									</el-col>
									<el-col :span="24">
										<el-form-item label="平台抽成:" prop="cType" :rules="MUSTINFO">
											<el-radio-group v-model="channelModel.cType">
												<el-radio label="0">关闭</el-radio>
												<el-radio label="1">每单固定抽成</el-radio>
												<el-radio label="2">按订单比例抽成</el-radio>
											</el-radio-group>
										</el-form-item>
									</el-col>
									<el-col :span="24" v-if="channelModel.cType==1">
										<el-form-item label="每单固定抽成:" prop="onePrice" id="oneP">
											<el-input v-model="channelModel.onePrice" size="small"></el-input>
										</el-form-item>
									</el-col>
									<el-col :span="24" v-if="channelModel.cType==2">
										<el-form-item label="抽成比例:" prop="oneProportion">
											<el-input v-model="channelModel.oneProportion" size="small" style="width: 200px;"></el-input><span style="line-height: 2;font-weight:bold;margin-left:5px;">%</span>
										</el-form-item>
									</el-col>
									<el-col :span="24">
										<el-form-item label="绑定支付手机号:" prop="mobile" id="mobile" :rules="PHONE">
											<el-input v-model="channelModel.mobile" size="small"></el-input>
											<span style="line-height: 2;display: block;">支付手机号需先注册为商城账号，渠道商资金将每日定期发放到该手机绑定的微信账户中。</span>
										</el-form-item>
									</el-col>
								</el-form>
							</div>
						</div>
					</div>
					<div class="modal-footer">
						<el-button type="success" size="small" id="save" @click="save('channelModel')" :loading="loading">保存</el-button>
						<el-button type="success" size="small" id="usave" @click="uSave('channelModel')" :loading="loading">保存</el-button>
						<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					</div>
				</div>
			</div>
			<div></div>
		</div>
	</div>
</template>
<script>
	import axios from "axios"
	import { formatDate } from 'static/js/date.js'
	import qs from "qs"
	import { MUSTNUMBER, MUSTINFO, PHONE } from 'static/js/rules.js'
	export default {
		data() {
			var onePriceVal = (rule, value, callback) => {
				if(this.channelModel.cType == 1) {
					if(value == "" || value == null) {
						return callback(new Error('该项为必填'));
					} else {
						callback()
					}
				}
			};
			var oneProportionVal = (rule, value, callback) => {
				if(this.channelModel.cType == 2) {
					if(value == "" || value == null) {
						return callback(new Error('该项为必填'));
					} else {
						callback()
					}
				}
			};
			return {
				purl: this.$store.state.purl, //权限数组
				url: this.GLOBAL.url,
				imgurl: this.GLOBAL.imgurl,
				currentPage: 1, //当前页
				totals: 0, //分页总条数
				size: 0, //每页条数
				totalPage: 0, //总页数
				inputPage: 1,
				channelLists: [],
				addShow: 0,
				MUSTNUMBER,
				MUSTINFO,
				PHONE,
				loading: false,
				updateShow: 0,
				id: '',
				channelModel: {
					name: '',
					onePrice: '',
					oneProportion: '',
					remark: '',
					mobile: '',
					cType: "0"
				},
				channelRul: {
					onePrice: [{
						validator: onePriceVal,
						trigger: 'blur'
					}],
					oneProportion: [{
						validator: oneProportionVal,
						trigger: 'blur'
					}],
				}
			}
		},
		activated() {
			this.load(1);
			this.addShow = this.purl[0].findIndex((val) => val === "/interface/channel/add") //判断是否拥有增加权限
			this.updateShow = this.purl[0].findIndex((val) => val === "/interface/channel/view") //判断是否拥有更新权限

		},
		methods: {
			load(num) {
				axios.get("/interface/channel/list?pageNo=" + num).then((res) => {
					this.channelLists = res.data.data.list;
					this.totals = res.data.data.pageBean.totalRow; //总条数
					this.size = res.data.data.pageBean.pageSize; //每页条数
					this.totalPage = res.data.data.pageBean.totalPage; //总页数
				})

			},
			add() {
				$("#channelAdd").modal('show');
				$("#save").show();
				$("#usave").hide();
				this.channelModel = {
					name: '',
					onePrice: '',
					oneProportion: '',
					remark: '',
					mobile: '',
					cType: "0"
				}
			},
			save(formname) {
				this.$refs[formname].validate((valid) => {
					if(valid) {
						this.loading = true;
						axios({
							method: 'post',
							url: '/interface/channel/add',
							data: this.channelModel,
							headers: {
								'Content-Type': 'application/json'
							}
						}).then((res) => {
							if(res.data.code == 1) {
								this.loading = false;
								$("#channelAdd").modal("hide");
								this.load(this.currentPage);
								this.$message.success("新增成功")
							} else {
								this.loading = false;
								var msg = res.data.msg;
								$("#channelAdd").modal("hide");
								this.$alert(msg, '提示消息', {
									confirmButtonText: '确定',
									callback: action => {
										$("#channelAdd").modal("show");
									}
								});
							}
						});
					}
				})
			},
			update(index, id) {
				$("#channelAdd").modal('show');
				$("#save").hide();
				$("#usave").show();
				this.id = id;
				axios.get("/interface/channel/view?id=" + id).then((res) => {
					this.channelModel = res.data.data
					this.channelModel.cType = res.data.data.cType.toString()
				})
			},
			uSave(formname) {
				this.$refs[formname].validate((valid) => {
					if(valid) {
						this.channelModel.id = this.id;
						this.loading = true;
						axios({
							method: 'post',
							url: '/interface/channel/add',
							data: this.channelModel,
							headers: {
								'Content-Type': 'application/json'
							}
						}).then((res) => {
							if(res.data.code == 1) {
								this.loading = false;
								$("#channelAdd").modal("hide");
								this.load(this.currentPage);
								this.$message.success("新增成功")
							} else {
								this.loading = false;
								var msg = res.data.msg;
								$("#channelAdd").modal("hide");
								this.$alert(msg, '提示消息', {
									confirmButtonText: '确定',
									callback: action => {
										$("#channelAdd").modal("show");
									}
								});
							}
						});
					}
				})
			},
			handleCurrentChange(val) {
				this.currentPage = val - 0;
				this.inputPage = val;
				this.load(val)
			},
			dateFormat: function(row, column) {
				var date
				if(row[column.property]) {
					date = new Date(parseInt(row[column.property]))
					date = formatDate(date, 'yyyy-MM-dd hh:mm:ss')
				}
				if(date == undefined) {
					return "";
				}
				return date;
			},
		}
	}
</script>

<style>
	#channelAdd .modal-dialog {
		left: 35% !important;
		top: 5% !important;
	}
	
	#oneP .el-form-item__label {
		width: 103px !important;
	}
	
	#oneP .el-form-item__content {
		margin-left: 103px !important;
	}
	
	#mobile .el-form-item__label {
		width: 130px !important;
	}
	
	#mobile .el-form-item__content {
		margin-left: 130px !important;
	}
</style>